package com.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.DUtils.QueryPageParam;
import com.book.DUtils.Res;
import com.book.entity.Njxx;
import com.book.service.NjxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/book/njxx")
public class NjxxController {

    @Autowired
    NjxxService njxxService;

    @GetMapping("/list")
    public Res list(){
        return Res.sucess(njxxService.list());
    }

    @PostMapping("/listP")
    //模糊查询
    public Res listP(@RequestBody Njxx njxx){
        LambdaQueryWrapper<Njxx> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(njxx.getNjNjzym())){
            lambdaQueryWrapper.like(Njxx::getNjNjzym,njxx.getNjNjzym());
        }
        System.out.println(njxxService.list(lambdaQueryWrapper));
        return Res.sucess(njxxService.list(lambdaQueryWrapper));
    }

    @PostMapping("/listPage")
    public Res listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String) param.get("njNjzym");
        System.out.println(name);

        Page<Njxx> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        System.out.println(query.getPageNum()+" "+query.getPageSize());

        LambdaQueryWrapper<Njxx> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Njxx::getNjNjzym,name);
        }

        IPage result = njxxService.page(page,lambdaQueryWrapper);
        System.out.println(result.getTotal());
        return Res.sucess(result.getRecords(),result.getTotal());
    }

    @PostMapping("/listPageC")
    public List<Njxx> listPageC(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String) param.get("njNjzym");
        System.out.println(name);

        Page<Njxx> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Njxx> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Njxx::getNjNjzym,name);

        IPage result = njxxService.pageC(page,lambdaQueryWrapper);
        System.out.println(result.getTotal());
        return result.getRecords();
    }

    @DeleteMapping("/delete/{id}")
    public Res delete(@PathVariable Integer id){
        boolean b = njxxService.removeById(id);
        if (b){
            return Res.sucess("success");
        }else
            return Res.fail();
    }
}

